package edu.ccut.saturn.studio.wfg.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.wfgmodel.ExtendAttributeForFormData;
import edu.ccut.saturn.studio.wfg.wfgmodel.ExtendAttributeForLogicKey;
import edu.ccut.saturn.studio.wfg.wfgmodel.FormDataAttribute;
import edu.ccut.saturn.studio.wfg.wfgmodel.LogicKeyAttribute;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnAttribute;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnName;
import edu.ccut.saturn.studio.wfg.wfgmodel.UsersAttribute;

public class ChildLoader extends NodeLoader{

	public List<Shape> loadNodes(Element root) {
		return null;
	}
	
	public SaturnAttribute getForm(Node dataNode) {
		FormDataAttribute form = new FormDataAttribute();
		if(dataNode == null){
			return null;
		}
		form.setId(super.getNodeAttributeValue(dataNode, SaturnName.KEY_NODE_NAME_ID));
		form.setAccessType(super.getNodeAttributeValue(dataNode, SaturnName.KEY_NODE_NAME_ACCESSTYPE));
		form.setKey(super.getNodeAttributeValue(dataNode, SaturnName.KEY_NODE_NAME_KEY));
		form.setRequired(super.getNodeAttributeValue(dataNode, SaturnName.KEY_NODE_NAME_REQUIRED));
		form.setType(super.getNodeAttributeValue(dataNode, SaturnName.XML_TYPE));
		form.setName(super.getNodeAttributeValue(dataNode, Shape.KEY_NODE_NAME));
		
		NodeList nodeList = dataNode.getChildNodes();
		List<SaturnAttribute> list = new ArrayList<SaturnAttribute>();
		for(int i = 0 ;i<nodeList.getLength();i++){
			Node node = nodeList.item(i);
			if(node.getNodeName().equals(SaturnName.KEY_NODE_NAME_PROPERTY)){
				ExtendAttributeForFormData tian = new ExtendAttributeForFormData();
				tian.setOptionItem(SaturnW3CUtil.getNodeAttributeValue(node, SaturnName.KEY_NONE_NAME_VALUE));
//暂时去掉，不要删				tian.setIfChecked(ModelXmlUtils.getNodeAttributeValue(node, SaturnName.ASSOCIATE));
				tian.setOptionValue(node.getFirstChild().getNodeValue());
				list.add(tian);
			}
		}
		form.setAttributes(list);
		return form;
		}
	
	public SaturnAttribute getUser(Node node) {
		UsersAttribute user = new UsersAttribute();
		if(node == null){
			return null;
		}
		user.setId(super.getNodeAttributeValue(node, SaturnName.KEY_NODE_NAME_ID));
		user.setName(super.getNodeAttributeValue(node, Shape.KEY_NODE_NAME));
		user.setType(super.getNodeAttributeValue(node, SaturnName.XML_TYPE));
		return user;
	}
	
	public SaturnAttribute getKey(Node node) {
		LogicKeyAttribute logic = new LogicKeyAttribute();
		if(node == null){
			return null;
		}
		logic.setKey(super.getNodeAttributeValue(node, SaturnName.KEY_NODE_NAME_FULLKEY));
		
		NodeList nodeList = node.getChildNodes();
		List<SaturnAttribute> list = new ArrayList<SaturnAttribute>();
		for(int i = 0; i < nodeList.getLength(); i++){
			Node node1 = nodeList.item(i);
			if(node1.getNodeName().equals(SaturnName.KEY_NODE_NAME_PARAMETER)){
				ExtendAttributeForLogicKey logicK = new ExtendAttributeForLogicKey();
				logicK.setId(SaturnW3CUtil.getNodeAttributeValue(node1, SaturnName.KEY_NODE_NAME_ID));
				
				logicK.setParameter(node1.getFirstChild().getNodeValue());
				list.add(logicK);
			}
		}
		
		logic.setAttributes(list);
		return logic;
	}
}
